package xyz.bali16.module.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.article.entity.Article;
import xyz.bali16.module.article.entity.Collect;
import xyz.bali16.module.article.entity.Collect;
import xyz.bali16.module.article.model.collection.CollectionAddParam;
import xyz.bali16.module.article.model.collection.CollectionPageParam;
import xyz.bali16.module.article.service.ArticleService;
import xyz.bali16.module.article.service.CollectionService;
import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.article.utils.ResultUtils.errorLog;


@RestController
@RequestMapping(BaseController.API+"collection")
@Api(tags = "收藏接口")
@AllArgsConstructor
public class CollectionController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(CollectionController.class);
    @Autowired
    CollectionService collectionService;
    @Autowired
    ArticleService articleService;

    @GetMapping
    @ApiOperation("分页条件查询收藏")
    //@PreAuthorize("@auth.permission('plugin:article:collection:query')")
    public Result<IPage<Collect>> pageQuery(@Valid CollectionPageParam param) {
        IPage<Collect> collectionIPage = collectionService.getPage(param);
        List<Collect> collects = collectionIPage.getRecords();
        for(Collect collect : collects){
            Article article = articleService.getById(collect.getTargetId());
            collect.setArticleTitle(article.getTitle());
            collect.setArticleContent(article.getContent());
        }
        return success(ApiEnum.GET_SUCCESS, collectionIPage);
    }
    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:article:collection:add')")
    @ApiOperation("添加收藏")
    public Result<String> add(@RequestBody @Valid CollectionAddParam param) {
        try {
            collectionService.addCollection(param);

            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增收藏 信息失败 {}", e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增收藏失败", e);
        }
    }

    @GetMapping("/count")
    //@PreAuthorize("@auth.permission('plugin:article:collection:count')")
    @ApiOperation("获取收藏数量")
    public Result<Integer> count() {
        try {
            return success(ApiEnum.GET_SUCCESS,collectionService.count());
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "查询收藏数量失败", e);
        }
    }

    @DeleteMapping("/{collectionId}")
    //@PreAuthorize("@auth.permission('plugin:article:collection:delete')")
    @ApiOperation("删除收藏")
    @ApiImplicitParam(name = "collectionId", value = "收藏id", paramType = "path", required = true)
    public Result<String> deleteCollection(@PathVariable("collectionId") String collectionId) {
        try {
            collectionService.delete(collectionId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除收藏 '{}' 失败. {}", collectionId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
